﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kimd.Motion
{
    public class DigitalInputStep:Step
    {
        public string SensorName { get; set; }
        public bool OnOff { get; set; }
        public DigitalInputStep()
        {

        }
        public DigitalInputStep(string description, StepType stepType)
            : base(description, stepType)
        {

        }
        public override bool Run(Dictionary<string, Axis> machineAxis, Dictionary<string, Cylinder> machineCylinder, Dictionary<string, Sensor> machineSensor, out string errInfo)
        {
            errInfo = "";
            if (OnOff)
            {
                if (!machineSensor[SensorName].WaitOn(2000))
                {
                    errInfo = $"{SensorName} ON 超时";
                    return false;
                }
            }
            else
            {
                if (!machineSensor[SensorName].WaitOn(2000,false))
                {
                    errInfo = $"{SensorName} OFF 超时";
                    return false;
                }
            }
            return true;
        }

        public override TreeNode ToTreeNode()
        {
            TreeNode node = new TreeNode();
            node.Text = this.ToString();
            node.Tag = this;
            return node;
        }
        public override string ToString()
        {
            return OnOff ? $"{this.Description}-[{SensorName} ON]" : $"{this.Description}-[{SensorName} OFF]";
        }
    }
}
